/// <reference path="../../typings/index.d.ts" />
'use strict';
const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const UserInfo = mongoose.model('UserInfo');

module.exports = {
    nameEixst: (req, res, next) => {
        const { username } = req.body;
        UserInfo.find({ username: username }, (err, user) => {
            if (err) {
                return res.json({
                    err: err,
                    msg: '获取数据失败，请重新尝试！',
                    data: null
                })
            };
            if (user.length < 1) {
                return res.json({
                    err: new Error('用户名不存在！'),
                    msg: '用户名不存在！',
                    data: null
                })
            };
            next();
        })
    },
    loginEixst: (req, res, next) => {
        const { username, userpass } = req.body;
        UserInfo.find({ username: username, userpass: userpass }, (err, user) => {
            if (err) {
                return res.json({
                    err: err,
                    msg: '获取数据失败，请重新尝试！',
                    data: null
                })
            };
            if (user.length < 1) {
                return res.json({
                    err: new Error('密码错误!'),
                    msg: '密码错误!',
                    data: null
                })
            };
            next();
        })
    },
    userLogin: (req, res, next) => {
        const { username, userpass } = req.body;
        req.session.username = username;
        req.session.userpass = userpass;
        return res.json({
            err: null,
            msg: '登录成功，即将跳转！',
            data: { username, userpass }
        })
    }
}